package com.cndym.servlet;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.cndym.utils.DESPlus;

/**
 * 
 *  userCode升级,一律转化成token,此token有时效性
 * 
 * 
 */
public class TokenUsercodeChangeFilter implements Filter {
	 
	DESPlus des = null;
	private Logger logger = Logger.getLogger(getClass());

	public TokenUsercodeChangeFilter() {

	}

	public void destroy() {
	}

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest)req;
        HttpServletResponse response = (HttpServletResponse)resp;
        logger.info(">>>>>>>>>>>>>>>开始Token和userCode的转换.");
        response.reset();
        ChangeRequestWarpper crw=new ChangeRequestWarpper(request);
        ChangeResponseWarpper responseWarpper=new ChangeResponseWarpper(response);
        chain.doFilter(crw,responseWarpper);
        //responseWarpper.getWriter().flush();

    }

    public void init(FilterConfig config) throws ServletException {
    	 try {
    		  new Thread(new Runnable() {
                  @Override
                  public void run() {
                	  while(true)
                     if(!CheckFrameWriter.IsmemCacheOK)
                     {
                    	 logger.info("memCache非正常运行");
                    	 try {
							Thread.sleep(1000*60*10);//10分钟
							CheckFrameWriter.IsmemCacheOK=true;
							logger.info("memCache设置正常运行成功");
						} catch (InterruptedException e) {
							
						}
                     }
                     else{
                    	 try {
                    		 logger.info("memCache正常运行或者程序置为成功");
							Thread.sleep(1000*60*10);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
                     }
                  }
              }).start();
 			//des=new DESPlus();
 		} catch (Exception e) {
 			e.printStackTrace();
 		}
    }

}
